﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Xml;
using System.Collections.Generic;
using System.Windows.Media.Imaging;

namespace Holidays
{
     
    public class save
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream = null;
        XDocument Xdoc = null;

        string holidayPlanPath = "Holidays/HolidayPlans.xml";
        string HolidayPath = "SampleData/Holiday.xml";
        string PlacePath = "SampleData/Places.xml";
        public Holidayprop LoadHolidays(string holid, string mnthid)
        {
            Holidayprop objFestProp = new Holidayprop();


            if (isoStore.FileExists(holidayPlanPath))
            {
                isoStream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Open, isoStore);
                Xdoc = XDocument.Load(isoStream);
                isoStream.Close();


                var CheckHolId = from i in Xdoc.Descendants("holidayplan") select i;
                foreach (var checkId in CheckHolId)
                {
                    var holidElements = from i in checkId.Descendants("plan").Descendants("holid") where i.Value == holid select i;

                    if (holidElements.Count() > 0)
                    {
                        foreach (var holidElement in holidElements)
                        {

                            if (holidElement.Value == holid)
                            {
                                objFestProp.HolidayImage = LoadHolidayImage(holid, mnthid, "imagenotselected");
                                objFestProp.HolId = holid;
                                break;
                            }
                            else
                            {
                                objFestProp.HolidayImage = LoadHolidayImage(holid, mnthid, "imageselected");
                                objFestProp.HolId = holid;
                                break;
                            }
                        }
                    }
                    else
                    {
                        objFestProp.HolidayImage = LoadHolidayImage(holid, mnthid, "imageselected");
                        objFestProp.HolId = holid;
                        break;
                    }
                }


            }
            else
            {
                objFestProp.HolidayImage = LoadHolidayImage(holid, mnthid, "imageselected");
                objFestProp.HolId = holid;
            }

            return objFestProp;
        }

        private ImageSource LoadHolidayImage(string id, string mid, string imgType)
        {
            BitmapImage objBitMap = new BitmapImage();

            Xdoc = XDocument.Load(HolidayPath);

            var holDetails = from i in Xdoc.Descendants("Holiday") where i.Attribute("id").Value == id select i;

            foreach (var hDetails in holDetails)
            {
                string mnth1 = hDetails.Element("date").Value;
                int d = mnth1.IndexOf("/");
                string mnth2 = mnth1.Substring(0, d);
                if (hDetails.Element(imgType) != null && mnth2 == mid)
                    objBitMap.UriSource = new Uri(hDetails.Element(imgType).Value, UriKind.Relative);
                break;
            }

            return objBitMap;
        }
        private ImageSource LoadHolidayImage1(string id, string imgType)
        {
            BitmapImage objBitMap = new BitmapImage();

            Xdoc = XDocument.Load(HolidayPath);

            var holDetails = from i in Xdoc.Descendants("Holiday") where i.Attribute("id").Value == id select i;

            foreach (var hDetails in holDetails)
            {

                if (hDetails.Element(imgType) != null)
                    objBitMap.UriSource = new Uri(hDetails.Element(imgType).Value, UriKind.Relative);
                break;
            }

            return objBitMap;
        }

        public List<PlacesProp> LoadPlaces()
        {
            List<PlacesProp> objPlacesList = new List<PlacesProp>();

            Xdoc = XDocument.Load("SampleData/Places.xml");
            var placDetails = from i in Xdoc.Descendants("place")
                              select i;
            foreach (var plac in placDetails)
            {
                PlacesProp p = new PlacesProp();
                BitmapImage objbitmapimage = new BitmapImage();
                objbitmapimage.UriSource = new Uri(plac.Element("image").Value, UriKind.Relative);
                p.PlaceImage = objbitmapimage;
                p.PlaceName = plac.Element("name").Value;
                p.PlaceId = plac.Attribute("id").Value;
                objPlacesList.Add(p);
            }

            return objPlacesList;
        }

        public void SaveHolidayPlan(string HolId, string placeList)
        {
            XElement placeIdEle = new XElement("places");

            placeIdEle.Add(new XElement("placeid", placeList));

            if (isoStore.FileExists(holidayPlanPath))
            {
                isoStream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Open, isoStore);
                Xdoc = XDocument.Load(isoStream);
                isoStream.Close();

                var CheckFestId = from i in Xdoc.Descendants("plan") orderby Convert.ToInt32(i.Attribute("id").Value) descending select i;
                string NextId = "1";
                foreach (var checkId in CheckFestId)
                {
                    NextId = (Convert.ToInt32(checkId.Attribute("id").Value) + 1).ToString();
                    break;
                }

                Xdoc.Root.Add(new XElement("plan",
                         new XAttribute("id", NextId),
                         new XElement("holid", HolId),
                         new XElement(placeIdEle)));

                isoStream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Create, isoStore);
                Xdoc.Save(isoStream);
                isoStream.Close();
            }
            else
            {
                isoStream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Create, isoStore);
                Xdoc = new XDocument(new XElement("holidayplan",
                         new XElement("plan",
                         new XAttribute("id", "1"),
                         new XElement("holid", HolId),
                         new XElement(placeIdEle))));
                Xdoc.Save(isoStream);
                isoStream.Close();
            }
        }

        public List<HolidayPlanProp> LoadHolidayplanList()
        {
            string place = "";
            List<HolidayPlanProp> objHolidayPlanList = new List<HolidayPlanProp>();

            if (isoStore.FileExists(holidayPlanPath))
            {
                IsolatedStorageFileStream istream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Open, isoStore);
                istream.Flush();
                Xdoc = XDocument.Load(istream);
                istream.Close();

                var holidayList = from t in Xdoc.Descendants("plan") select t;

                foreach (var holiday in holidayList)
                {
                    HolidayPlanProp objProp = new HolidayPlanProp();

                    objProp.HolidayId = holiday.Attribute("id").Value;
                    objProp.HolidayImage = LoadHolidayImage1(holiday.Element("holid").Value, "imageselected");

                    string PlaceName = "";

                    foreach (var placeids in holiday.Elements("places"))
                    {
                        place = placeids.Element("placeid").Value;
                        string[] words = place.Split('/');
                        for (int i = 0; i < words.Length; i++)
                        {

                            if (PlaceName == "")
                                PlaceName = GetPlaceNames(words[i].ToString());
                            else
                                PlaceName = PlaceName + " / " + GetPlaceNames(words[i].ToString());
                        }
                    }
                    string[] word = PlaceName.Split('/');
                    objProp.PlaceName = String.Join(Environment.NewLine, word); ;
                    objHolidayPlanList.Add(objProp);
                }
            }
            return objHolidayPlanList;
        }

        private string GetPlaceNames(string placeId)
        {
            string PlaceName = "";
            Xdoc = XDocument.Load(PlacePath);

            var placeDetails = from i in Xdoc.Descendants("place") where i.Attribute("id").Value == placeId select i;

            foreach (var pDetails in placeDetails)
            {
                if (pDetails.Element("name") != null)
                    PlaceName = pDetails.Element("name").Value;
                break;
            }
            return PlaceName;
        }
        public bool checkId(string id)
        {
            if (isoStore.FileExists(holidayPlanPath))
            {
                isoStream = new IsolatedStorageFileStream(holidayPlanPath, FileMode.Open, isoStore);
                Xdoc = XDocument.Load(isoStream);
                isoStream.Close();


                var CheckFestId = from i in Xdoc.Descendants("holidayplan") select i;
                foreach (var checkId in CheckFestId)
                {
                    var holidElements = from i in checkId.Descendants("plan").Descendants("holid") where i.Value == id select i;

                    if (holidElements.Count() > 0)
                    {
                        foreach (var holidElement in holidElements)
                        {

                            if (holidElement.Value == id)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
            }
            return false;
        }

    }
}
